package com.boot.unity.api.modules.rzgjj.controller;

import com.alibaba.fastjson.JSONObject;
import com.boot.unity.api.common.AES;
import com.boot.unity.api.common.Result;
import com.boot.unity.api.modules.rzgjj.dao.GjjSeachLogDao;
import com.boot.unity.api.modules.rzgjj.service.GjjSeachLogService;
import com.boot.unity.api.modules.rzgjj.service.GjjService;
import com.boot.unity.api.utils.IpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/rzgjj")
public class GjjController {


    @Autowired
    private GjjService gjjService;


    @Value("${gjj-api.encryption}")
    private String encryption;


    @Value("${gjj-api.isResultFlag}")
    private boolean isResultFlag;

    @Autowired
    private GjjSeachLogService gjjSeachLogService;


    public static String changeType(String typeCode){
        if(typeCode.equals("01")){
            return "1001";
        }
        return typeCode;
    }



    // 职工银行卡有效性检验
    @PostMapping("/BAS001")
    public Object post_BAS001(@RequestBody String params,HttpServletRequest request){
        JSONObject objJson = (JSONObject) JSONObject.parse(params);
        String param = objJson.getString("ciphertext");
        String key = objJson.getString("key");

        String ipAddr = IpUtil.getIpAddress(request);
        String body = AES.decrypt(param,key);
//        System.out.println("请求接口位置：BAS001"+body);
        JSONObject json = JSONObject.parseObject(body);

        if(StringUtils.isEmpty(json.getString("pername"))){
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","持卡人姓名不能为空");
            return AES.encrypt(result.toString(),key);
        }
        if(StringUtils.isEmpty(json.getString("codetype"))){
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","证件类型不能为空");
            return AES.encrypt(result.toString(),key);
        }

        if(StringUtils.isEmpty(json.getString("codeno"))){
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","证件号码不能为空");
            return AES.encrypt(result.toString(),key);
        }
        if(StringUtils.isEmpty(json.getString("bankcode"))){
//            return new Result("400","银行卡号不能为空");
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","银行卡号不能为空");
            return AES.encrypt(result.toString(),key);
        }
        if(StringUtils.isEmpty(json.getString("accounttype"))){
//            return new Result("400","是否一类账户不能为空");
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","是否一类账户不能为空");
            return AES.encrypt(result.toString(),key);
        }
        String pername = json.getString("pername");
        String codetype = json.getString("codetype");
        String codeno = json.getString("codeno");
        String bankcode = json.getString("bankcode");
        String accounttype = json.getString("accounttype");

        codetype = changeType(codetype);

        int pageSize = 0;
        if(StringUtils.isEmpty(json.getString("pageSize"))){
            pageSize = 10;
        }else{
            pageSize = json.getInteger("pageSize");
        }
        int currentPage = 0;
        if(StringUtils.isEmpty(json.getString("currentPage"))){
            currentPage = 1;
        }else{
            currentPage = json.getInteger("currentPage");
        }
//        System.out.println("yyds::::"+encryption);
        JSONObject resultJson = gjjService.queryBAS001(pername, codetype, codeno, bankcode, accounttype, pageSize, currentPage);
//        System.out.println("resultJson:::::"+resultJson.toString());
        LocalDateTime nowDate = LocalDateTime.now();
        log.error("请求地址："+ipAddr+",请求参数："+body+",服务器时间:"+ nowDate+",请求结果："+resultJson);
        gjjSeachLogService.insertAll("BAS001",ipAddr,body,resultJson.toString(),nowDate.toString());


//        String result = AES.encrypt(param,encryption);
        if (isResultFlag) {
            return AES.encrypt(resultJson.toString(),key);
        } else {
            return resultJson;
        }
    }

    /*
    商业银行贷款合同信息
    lN1IFpYzh13tLwkOinODRfSNONKCFWhO4SkQguApA8IhDubGdtRiMhzlMJJRc4gH5l/l0fFRCken7MIMGKVupnP5gtQOyL0z8id+vcayn1q86FfmKgg8CpQh5CjMwcyY
     */
    @PostMapping("/BLS001")
    public Object post_BLS001(@RequestBody String params,HttpServletRequest request){
        JSONObject objJson = (JSONObject) JSONObject.parse(params);
        String param = objJson.getString("ciphertext");
        String key = objJson.getString("key");


//        System.out.println(encryption+"||||sss请求接口位置：BLS001----"+param);
//        System.out.println(AES.encrypt(param, encryption));
        String ipAddr = IpUtil.getIpAddress(request);
        String body = AES.decrypt(param,key);
//        System.out.println("请求接口位置：BLS001"+body);
        JSONObject json = JSONObject.parseObject(body);
        if(StringUtils.isEmpty(json.getString("pername"))){
//            return new Result("400","姓名不能为空");
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","姓名不能为空");
            return AES.encrypt(result.toString(),key);
        }
        if(StringUtils.isEmpty(json.getString("codetype"))){
//            return new Result("400","证件类型不能为空");
            /*JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","证件类型不能为空");
            return AES.encrypt(result.toString(),key);*/
            json.put("codetype","01");
        }
        if(StringUtils.isEmpty(json.getString("cardcode_2"))){
//            return new Result("400","证件号码不能为空");
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","证件号码不能为空");
            return AES.encrypt(result.toString(),key);
        }

//        codetype = changeType(codetype);

        int pageSize = 0;
        if(StringUtils.isEmpty(json.getString("pageSize"))){
            pageSize = 10;
        }else{
            pageSize = json.getInteger("pageSize");
        }
        int currentPage = 0;
        if(StringUtils.isEmpty(json.getString("currentPage"))){
            currentPage = 1;
        }else{
            currentPage = json.getInteger("currentPage");
        }
        String pername = json.getString("pername");
        String codetype = json.getString("codetype");
        String codeno = json.getString("cardcode_2");

        codetype = changeType(codetype);

        JSONObject resultJson = gjjService.queryBLS001(pername,codetype,codeno,pageSize,currentPage);
//        System.out.println("resultJson:::::"+resultJson.toString());
        LocalDateTime nowDate = LocalDateTime.now();
        log.error("请求地址："+ipAddr+",请求参数："+body+",服务器时间:"+ nowDate+",请求结果："+resultJson);
        gjjSeachLogService.insertAll("BLS001",ipAddr,body,resultJson.toString(),nowDate.toString());

        if (isResultFlag) {
            return AES.encrypt(resultJson.toString(),key);
        } else {
            return resultJson;
        }
    }


    //lN1IFpYzh13tLwkOinODRfSNONKCFWhO4SkQguApA8I8sRBHCwfcNhp3OTfjz9D/ye/VfWJAuT1hP1FYPJMWTC8Ik9joX8Z/gZ2NN7XGt3jN0hxNxD8lPcK2EppOQtjuMv1S8zAjkUDDSrVegGWDUKoaqHKsrxvBi6h2Qo7ROqioFISc/sfvS1NcFvEhlXM5DnnC0k/2GYq02AgYzHCHFizC4BwCrqAPpTNnPGXgFP8=    @PostMapping("/BLS002")
    @PostMapping("/BLS002")
    public Object post_BLS002(@RequestBody String params,HttpServletRequest request){
        JSONObject objJson = (JSONObject) JSONObject.parse(params);
        String param = objJson.getString("ciphertext");
        String key = objJson.getString("key");


        String ipAddr = IpUtil.getIpAddress(request);
        String body = AES.decrypt(param,key);
//        System.out.println("请求接口位置：BLS002"+body);
        JSONObject json = JSONObject.parseObject(body);
       /* if(StringUtils.isEmpty(json.getString("cardcode"))){
//            return new Result("400","证件号码不能为空");
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","证件号码不能为空");
            return AES.encrypt(result.toString(),key);
        }
        if(StringUtils.isEmpty(json.getString("pername"))){
//            return new Result("400","姓名不能为空");
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","姓名不能为空");
            return AES.encrypt(result.toString(),key);
        }*/
        if(StringUtils.isEmpty(json.getString("agrcode"))){
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","借款合同号不能为空");
            return AES.encrypt(result.toString(),key);
        }
        if(StringUtils.isEmpty(json.getString("day_id"))){
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","日期不能为空");
            return AES.encrypt(result.toString(),key);
        }
        /*if(StringUtils.isEmpty(json.getString("begindate"))){
//            return new Result("400","开始时间不能为空");
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","开始时间不能为空");
            return AES.encrypt(result.toString(),key);
        }
        if(StringUtils.isEmpty(json.getString("enddate"))){
//            return new Result("400","开始时间不能为空");
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","结束时间不能为空");
            return AES.encrypt(result.toString(),key);
        }*/
        int pageSize = 0;
        if(StringUtils.isEmpty(json.getString("pageSize"))){
            pageSize = 10;
        }else{
            pageSize = json.getInteger("pageSize");
        }
        int currentPage = 0;
        if(StringUtils.isEmpty(json.getString("currentPage"))){
            currentPage = 1;
        }else{
            currentPage = json.getInteger("currentPage");
        }
        String day_id = json.getString("day_id");
        String cardcode = json.getString("cardcode");
        if(StringUtils.isEmpty(json.getString("cardcode"))){
            cardcode = "";
        }
        String pername = json.getString("pername");
        if(StringUtils.isEmpty(json.getString("pername"))){
            pername = "";
        }
        String codetype = json.getString("codetype");
        if(StringUtils.isEmpty(json.getString("codetype"))){
            codetype = "";
        }
        codetype = changeType(codetype);
        String agrcode = json.getString("agrcode");
        String begindate = day_id.split("'and'")[0];//json.getString("begindate");
        String enddate = day_id.split("'and'")[1];//json.getString("enddate");
//        JSONObject resultJson = gjjService.queryBLS002(cardcode,pername,agrcode,begindate,enddate,pageSize,currentPage,codetype);
        JSONObject resultJson = gjjService.queryBLS002ForY(cardcode,pername,agrcode,begindate,enddate,pageSize,currentPage,codetype);
//        System.out.println("resultJson:::::"+resultJson.toString());
        LocalDateTime nowDate = LocalDateTime.now();
        log.error("请求地址："+ipAddr+",请求参数："+body+",服务器时间:"+ nowDate+",请求结果："+resultJson);
        gjjSeachLogService.insertAll("BLS002",ipAddr,body,resultJson.toString(),nowDate.toString());
        if (isResultFlag) {
            return AES.encrypt(resultJson.toString(),key);
        } else {
            return resultJson;
        }
    }

    //lN1IFpYzh13tLwkOinODRfSNONKCFWhO4SkQguApA8I8sRBHCwfcNhp3OTfjz9D/ye/VfWJAuT1hP1FYPJMWTJSPx4LoBPr676S3UTBbXvVqt51xGVx2V5MEW9t+xmS1huyjZVvYeGAjyAi6n+HI3XkSumyLn5syYfxCzkvAOq7DoXSPIrZcsjpNEU04NWAHQnAr7Bv9EQSsSz5QltsY85yHb2vQIuEbcIZuZu/fWcw=
    @PostMapping("/BLS003")
    public Object post_BLS003(@RequestBody String params, HttpServletRequest request){
        JSONObject objJson = (JSONObject) JSONObject.parse(params);
        String param = objJson.getString("ciphertext");
        String key = objJson.getString("key");



        String ipAddr = IpUtil.getIpAddress(request);
        String body = AES.decrypt(param,key);
//        System.out.println("请求接口位置：BLS003"+body);
        JSONObject json = JSONObject.parseObject(body);
        /*if(StringUtils.isEmpty(json.getString("cardcode"))){
//            return new Result("400","证件号码不能为空");
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","证件号码不能为空");
            return AES.encrypt(result.toString(),key);
        }
        if(StringUtils.isEmpty(json.getString("pername"))){
//            return new Result("400","姓名不能为空");
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","姓名不能为空");
            return AES.encrypt(result.toString(),key);
        }*/
        if(StringUtils.isEmpty(json.getString("agrcode"))){
//            return new Result("400","借款合同号不能为空");
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","借款合同号不能为空");
            return AES.encrypt(result.toString(),key);
        }
        if(StringUtils.isEmpty(json.getString("day_id"))){
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","日期不能为空");
            return AES.encrypt(result.toString(),key);
        }
        /*if(StringUtils.isEmpty(json.getString("begindate"))){
//            return new Result("400","开始时间不能为空");
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","开始时间不能为空");
            return AES.encrypt(result.toString(),key);
        }
        if(StringUtils.isEmpty(json.getString("enddate"))){
//            return new Result("400","结束时间不能为空");
            JSONObject result = new JSONObject();
            result.put("code","400");
            result.put("msg","结束时间不能为空");
            return AES.encrypt(result.toString(),key);
        }*/
        int pageSize = 0;
        if(StringUtils.isEmpty(json.getString("pageSize"))){
            pageSize = 10;
        }else{
            pageSize = json.getInteger("pageSize");
        }
        int currentPage = 0;
        if(StringUtils.isEmpty(json.getString("currentPage"))){
            currentPage = 1;
        }else{
            currentPage = json.getInteger("currentPage");
        }
        String day_id = json.getString("day_id");

        String cardcode = json.getString("cardcode");
        if(StringUtils.isEmpty(json.getString("cardcode"))){
            cardcode = "";
        }
        String pername = json.getString("pername");
        if(StringUtils.isEmpty(json.getString("pername"))){
            pername = "";
        }
        String codetype = json.getString("codetype");
        if(StringUtils.isEmpty(json.getString("codetype"))){
            codetype = "";
        }
        codetype = changeType(codetype);
        String agrcode = json.getString("agrcode");
        String begindate = day_id.split("'and'")[0];//json.getString("begindate");
        String enddate = day_id.split("'and'")[1];//json.getString("enddate");
        JSONObject resultJson = gjjService.queryBLS003(cardcode,pername,agrcode,begindate,enddate,pageSize,currentPage,codetype);
//        System.out.println("resultJson:::::"+resultJson.toString());
        LocalDateTime nowDate = LocalDateTime.now();
        log.error("请求地址："+ipAddr+",请求参数："+body+",服务器时间:"+ nowDate+",请求结果："+resultJson);
        gjjSeachLogService.insertAll("BLS003",ipAddr,body,resultJson.toString(),nowDate.toString());
        if (isResultFlag) {
            return AES.encrypt(resultJson.toString(),key);
        } else {
            return resultJson;
        }
    }


//    @PostMapping("/test")
    /*public Object test(@RequestBody String param){
        System.out.println(encryption+"||||sss请求接口位置：test----"+param);
        System.out.println(AES.encrypt(param, encryption));
        return Result.success();
    }*/
}
